﻿using Net.Sz.Framework.Netty;
using Net.Sz.Framework.Netty.Buffer;
using Net.Sz.Framework.Szlog;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace SocketServer
{
    class Program
    {
        private static SzLogger log = SzLogger.getLogger();

        static void Main(string[] args)
        {
            /*优先创建处理客户端连接的 handler程序*/
            NettyPool.SessionHandler = new Handler();
            /*开启socket的监听*/
            NettyPool.AddTcpBind("0.0.0.0", 9527);
            log.Info("我是");
            Console.ReadLine();
        }


        /// <summary>
        /// 处理客户端连接的 handler程序
        /// </summary>
        class Handler : IIOSessionHandler
        {
            /// <summary>
            /// 当客户端有新的连接，连接进来
            /// </summary>
            /// <param name="session"></param>
            public void ChannelActive(IOSession session)
            {

            }

            /// <summary>
            /// 服务器内部错误
            /// </summary>
            /// <param name="session"></param>
            /// <param name="ex"></param>
            public void ExceptionCaught(IOSession session, Exception ex)
            {

            }

            /// <summary>
            /// 收取客户端消息
            /// </summary>
            /// <param name="session">客户端连接对象</param>
            /// <param name="msgId">本次消息id</param>
            /// <param name="buffer">本次消息报文内容</param>
            public void ChannelRead(IOSession session, int msgId, byte[] buffer)
            {
                if (msgId == 2)
                {
                    string msg = "";
                    {
                        /*封装消息是为了读取消息方便*/
                        BufferReader br = new BufferReader(buffer);
                        /*读取发送过来的字符串，先发送的字符串，然后就先读取字符串*/
                        msg = br.ReadString();
                        /*读取图片数据,因为我们先发送一个string，然后读取一个string，剩下的所有字节都是图片的*/
                        byte[] imgBuff = br.ReadBytes((int)(br.BaseStream.Length - br.BaseStream.Position));

                        using (System.IO.MemoryStream stream = new System.IO.MemoryStream(imgBuff))
                        {
                            /*读取出来的图片*/
                            Bitmap img = new Bitmap((Image)new Bitmap(stream));
                        }

                    }
                    log.Error("收到客户端消息回复：" + session.ID + "  消息唯一回复id" + msg);
                    {
                        //回复消息
                        /*封装消息，方便书写消息，*/
                        BufferWriter bw = new BufferWriter();
                        /*书写一个long 类型数据*/
                        bw.Write(msg);
                        /*获取字节数组*/
                        byte[] buff = bw.GetBuffer();
                        /*向客户端连接发送消息*/
                        session.WriteAndFlush(2, buff);
                    }
                }
            }

            /// <summary>
            /// 链接断开
            /// </summary>
            /// <param name="session"></param>
            /// <param name="obj"></param>
            public void ChannelUnregistered(IOSession session, params string[] obj)
            {

            }

            /// <summary>
            /// 链接闲置状态
            /// </summary>
            /// <param name="session"></param>
            public void ChannelInactive(IOSession session)
            {

            }
        }
    }
}
